package com.itheima.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

/*
限流机制
1.确保Ack机制为手动确认  acknowledge="manual"
2.配置文件  prefetch="1"

 */
@Component
public class QosListener implements ChannelAwareMessageListener {

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();

        //获取消息
        System.out.println(new String(message.getBody()));
        //处理业务逻辑
        System.out.println("处理业务逻辑");

        //手动确认消费完毕
        channel.basicAck(deliveryTag,true);
    }
}
